<template>
  <div class="stock-in-management">
    <div class="page-title">入库记录管理</div>
    
    <!-- 搜索区域 -->
    <el-card class="search-card">
      <el-form :model="searchForm" :inline="true">
        <el-form-item label="物料编码">
          <el-input v-model="searchForm.MaterCode" placeholder="请输入物料编码" clearable />
        </el-form-item>
        <el-form-item label="物料名称">
          <el-input v-model="searchForm.MaterName" placeholder="请输入物料名称" clearable />
        </el-form-item>
        <el-form-item label="入库批次号">
          <el-input v-model="searchForm.In_BatchCode" placeholder="请输入批次号" clearable />
        </el-form-item>
        <el-form-item label="仓库名称">
          <el-input v-model="searchForm.WarehouseName" placeholder="请输入仓库名称" clearable />
        </el-form-item>
        <el-form-item label="入库时间">
          <el-date-picker
            v-model="searchForm.in_Time"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD"
            style="width: 240px" />
        </el-form-item>
        <!-- <el-form-item label="入库类型">
          <el-select v-model="searchForm.In_Type" placeholder="请选择" style="width: 120px" clearable>
            <el-option label="采购入库" value="采购入库" />
            <el-option label="生产入库" value="生产入库" />
            <el-option label="调拨入库" value="调拨入库" />
            <el-option label="退货入库" value="退货入库" />
            <el-option label="其他" value="其他" />
          </el-select>
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon> 搜索
          </el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 数据表格 -->
    <el-card>
      <el-table :data="stockInList" stripe v-loading="loading">
        <el-table-column prop="materCode" label="物料编码" width="120" />
        <el-table-column prop="materName" label="物料名称" width="150" />
        <el-table-column prop="materType" label="物料分类" width="100" />
        <el-table-column prop="in_BatchCode" label="入库批次号" width="140" />
        <el-table-column prop="warehouseName" label="仓库名称" width="120" />
        <el-table-column prop="locationName" label="库位名称" width="120" />
        <el-table-column prop="in_Quantity" label="入库数量" width="100" align="right">
          <template #default="scope">
            {{ formatNumber(scope.row.in_Quantity) }} <span v-if="scope.row.unit">{{ scope.row.unit }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="in_Type" label="入库类型" width="100" />
        <el-table-column prop="in_Time" label="入库时间" width="160">
          <template #default="scope">
            {{ formatDateTime(scope.row.in_Time) }}
          </template>
        </el-table-column>
        <el-table-column prop="siloName" label="筒仓名称" width="120" />
        <el-table-column label="操作" width="80" fixed="right">
          <template #default="scope">
            <el-button size="small" type="primary" @click="handleDetail(scope.row)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.current"
          v-model:page-size="pagination.size"
          :total="pagination.total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    
    <!-- 详情弹窗 -->
    <StockInDetail
      :visible="detailVisible"
      @update:visible="detailVisible = $event"
      :detail-data="currentDetail" />
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { Search } from '@element-plus/icons-vue'
import { request } from '@/utils/request'
import { ElMessage } from 'element-plus'

// 导入组件
import StockInDetail from './StockInDetail.vue'

// 响应式数据
const loading = ref(false)
const stockInList = ref([])
const detailVisible = ref(false)
const currentDetail = ref({})

// 搜索表单
const searchForm = reactive({
  MaterCode: '',
  MaterName: '',
  In_BatchCode: '',
  WarehouseName: '',
  In_Type: '',
  in_Time:[]
})

// 分页
const pagination = reactive({
  current: 1,
  size: 10,
  total: 0
})

// 格式化数字显示
const formatNumber = (value) => {
  if (value === null || value === undefined) return '0'
  return Number(value).toLocaleString()
}

// 格式化日期时间
const formatDateTime = (dateString) => {
  if (!dateString) return ''
  const date = new Date(dateString)
  return date.toLocaleString()
}

// 加载入库记录数据
const loadStockInData = async () => {
  loading.value = true
  try {
    const params = {
      filter: {
        PageIndex: pagination.current,
        PageSize: pagination.size,
        OrderBy: "In_Time",
        IsAscending: false
      },
      stockIn: {
        MaterCode: searchForm.MaterCode,
        MaterName: searchForm.MaterName,
        In_BatchCode: searchForm.In_BatchCode,
        WarehouseName: searchForm.WarehouseName,
        In_Type: searchForm.In_Type,
        InTimeScope:searchForm.in_Time && searchForm.in_Time.length > 0 ? searchForm.in_Time[0] +'至'+ searchForm.in_Time[1]: null
      }
    }
    
    // 调用后端API
    const response = await request.post('/StockIn/Page', params)
    stockInList.value = response.data
    pagination.total = response.totalCount
  } catch (error) {
    ElMessage.error('加载入库记录失败')
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = () => {
  pagination.current = 1
  loadStockInData()
}

// 重置
const handleReset = () => {
  Object.keys(searchForm).forEach(key => {
    searchForm[key] = ''
  })
  pagination.current = 1
  loadStockInData()
}

// 查看详情
const handleDetail = (row) => {
  currentDetail.value = { ...row }
  detailVisible.value = true
}

// 分页大小改变
const handleSizeChange = (size) => {
  pagination.size = size
  pagination.current = 1
  loadStockInData()
}

// 当前页改变
const handleCurrentChange = (page) => {
  pagination.current = page
  loadStockInData()
}

// 页面加载时获取数据
onMounted(() => {
  loadStockInData()
})
</script>

<style scoped>
.stock-in-management {
  padding: 20px;
}

.page-title {
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 20px;
  color: #303133;
}

.search-card {
  margin-bottom: 20px;
}

.pagination-container {
  margin-top: 20px;
  text-align: center;
}
</style>